package ace.module.oss.core.impl.service.impl;

import ace.cmp.core.constants.CoreConstant;
import ace.module.common.api.constant.CommonConstants;
import ace.module.oss.api.enums.OssApplicationStatusEnum;
import ace.module.oss.api.enums.OssSupplierTypeEnum;
import ace.module.oss.core.impl.dao.entity.OssApplication;
import ace.module.oss.core.impl.dao.entity.OssApplicationConfig;
import ace.module.oss.core.impl.dao.entity.OssApplicationSupplier;
import ace.module.oss.core.impl.dao.entity.OssSupplier;
import ace.module.oss.core.impl.dao.repository.OssApplicationConfigRepository;
import ace.module.oss.core.impl.dao.repository.OssApplicationRepository;
import ace.module.oss.core.impl.dao.repository.OssApplicationSupplierRepository;
import ace.module.oss.core.impl.dao.repository.OssSupplierRepository;
import ace.module.oss.core.impl.manager.OssSupplierManager;
import ace.module.oss.core.impl.service.OssInitService;
import java.time.Instant;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

/**
 * module init.
 *
 * @author Caspar
 */
@Slf4j
@AllArgsConstructor
@Component
public class OssInitServiceImpl implements OssInitService {

  private final OssApplicationRepository ossApplicationRepository;
  private final OssApplicationSupplierRepository ossApplicationSupplierRepository;
  private final OssSupplierRepository ossSupplierRepository;
  private final OssApplicationConfigRepository ossApplicationConfigRepository;
  private final OssSupplierManager ossSupplierManager;

  @Transactional(rollbackFor = Exception.class)
  @Override
  public void init() {
    OssApplication ossApplication =
        OssApplication.builder()
            .id(CommonConstants.DEFAULT_APP_ID)
            .key(CommonConstants.DEFAULT_APP_KEY)
            .name(CommonConstants.DEFAULT_TITLE)
            .status(OssApplicationStatusEnum.NORMAL.getCode())
            .remark(CommonConstants.DEFAULT_REMARK)
            .deleteFlag(CoreConstant.DELETE_FLAG_NOT)
            .createTime(Instant.now().toEpochMilli())
            .updateTime(Instant.now().toEpochMilli())
            .rowVersion(CoreConstant.DEFAULT_ROW_VERSION)
            .build();

    OssSupplier ossSupplier =
        OssSupplier.builder()
            .id(CommonConstants.DEFAULT_APP_ID)
            .name(CommonConstants.DEFAULT_TITLE)
            .type(OssSupplierTypeEnum.OSS_ALIYUN.getCode())
            .config(ossSupplierManager.getDefaultAliyunConfigWithoutSecret())
            .deleteFlag(CoreConstant.DELETE_FLAG_NOT)
            .createTime(Instant.now().toEpochMilli())
            .updateTime(Instant.now().toEpochMilli())
            .rowVersion(CoreConstant.DEFAULT_ROW_VERSION)
            .build();

    OssApplicationSupplier ossApplicationSupplier =
        OssApplicationSupplier.builder()
            .id(CommonConstants.DEFAULT_APP_ID)
            .ossAppId(ossApplication.getId())
            .ossSupplierId(ossSupplier.getId())
            .deleteFlag(CoreConstant.DELETE_FLAG_NOT)
            .createTime(Instant.now().toEpochMilli())
            .updateTime(Instant.now().toEpochMilli())
            .rowVersion(CoreConstant.DEFAULT_ROW_VERSION)
            .build();

    OssApplicationConfig ossApplicationConfig =
        OssApplicationConfig.builder()
            .id(CommonConstants.DEFAULT_APP_ID)
            .ossAppId(ossApplication.getId())
            .catalog("/img")
            .key("image")
            .deleteFlag(CoreConstant.DELETE_FLAG_NOT)
            .createTime(Instant.now().toEpochMilli())
            .updateTime(Instant.now().toEpochMilli())
            .rowVersion(CoreConstant.DEFAULT_ROW_VERSION)
            .build();

    ossApplicationRepository.insert(ossApplication);
    ossSupplierRepository.insert(ossSupplier);
    ossApplicationSupplierRepository.insert(ossApplicationSupplier);
    ossApplicationConfigRepository.insert(ossApplicationConfig);
  }
}
